﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.DirectX;

namespace BurningSphere.Simulator
{
	/// <summary>
	/// Loads a RAW 16bit X, 16bit Y shape data file
	/// </summary>
	public class ShapeRAW : Shape
	{
		public override void Load(string file)
		{

			byte[] bytes = File.ReadAllBytes(file);

			short lastIntX = 0;
			short lastIntY = 0;
			short intX = 0;
			short intY = 0;
			float lastFloatX = 0;
			float lastFloatY = 0;

			for (int i = 0; i < bytes.Length; i += 4)
			{
				if (i + 3 >= bytes.Length)
				{
					continue;
				}

				intX = BitConverter.ToInt16(bytes, i);
				intY = BitConverter.ToInt16(bytes, i + 2);

				if (lastIntX == intX && lastIntY == intY)
				{
					continue;
				}

				float x = ((float)intX) / 16000.0f;
				float y = ((float)intY) / 16000.0f;

				Vector2 delta = new Vector2(x - lastFloatX, y - lastFloatY);
				if (delta.Length() < 0.05f)
				{
					continue;
				}

				points.Add(
					new Vector2(x, y)
					);

				lastIntX = intX;
				lastIntY = intY;
				lastFloatX = x;
				lastFloatY = y;
			}
		}
	}
}
